#include <iostream>
#include <memory>
using namespace std;
class B;
class A {
public:
    A() { std::cout << "A()" << std::endl; }
    void test(){cout << "test A" << endl;}
    ~A() { std::cout << "~A()" << std::endl; }
    std::weak_ptr<B> _sptrB;
};

class B {
public:
    B() { std::cout << "B()" << std::endl; }
    void test(){cout << "test B" << endl;}
    ~B() { std::cout << "~B()" << std::endl; }
    std::weak_ptr<A> _sptrA;
};

int main() {
    std::shared_ptr<A> pA(new A);
    std::shared_ptr<B> pB(new B);

    pA->_sptrB = pB;
    pB->_sptrA = pA;

    std::cout << pA.use_count() << std::endl;
    std::cout << pB.use_count() << std::endl;

    pB->test();
    shared_ptr<B> tmp = pA->_sptrB.lock();
    if(tmp != nullptr){
        tmp->test();
    }
    cout << pA.use_count() << endl;
    cout << pB.use_count() << endl;

    tmp.reset();

    cout << pA.use_count() << endl;
    cout << pB.use_count() << endl;
}